<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
    <el-form-item label="客户" prop="customerId">
        <el-select
            v-model="queryParams.customerId"
            class="!w-240px"
            filterable
            clearable
            lable-key="name"
            placeholder="请选择客户"
            value-key="id"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.name"
              :value="item.id!"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="统计月份" prop="fillingDate">
        <el-date-picker
          v-model="queryParams.fillingDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="monthrange"
          start-placeholder="开始年月"
          end-placeholder="结束年月"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="投产日期" prop="productionDate">
        <el-date-picker
          v-model="queryParams.productionDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="monthrange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="退场日期" prop="exitDate">
        <el-date-picker
          v-model="queryParams.exitDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="monthrange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="投资性质" prop="investmentNature">
        <el-input
          v-model="queryParams.investmentNature"
          placeholder="请输入投资性质"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['crm:supporting-usage:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
              type="warning"
              plain
              @click="handleImport"
              v-hasPermi="['crm:supporting-usage:import']"
            >
              <Icon icon="ep:upload" /> 导入
            </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['crm:supporting-usage:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column align="center" label="客户" prop="customerName" width="300px">
        <template #default="scope">
          <el-link
            :underline="false"
            type="primary"
            @click="openCustomerDetail(scope.row.customerId)"
          >
            {{ scope.row.customerName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="统计月份"
        align="center"
        prop="fillingDate"
        :formatter="dateFormatter3"
        width="100px"
      />
      <el-table-column label="纯水用量" align="center" prop="pureWater" />
      <el-table-column label="自来水用量(不含打磨房)" align="center" prop="tapWater" />
      <el-table-column label="总用水量" align="center" prop="totalWater" />
      <el-table-column label="总用电量" align="center" prop="electricity" />
      <el-table-column label="蒸汽用量" align="center" prop="steam" />
      <el-table-column label="天然气用量" align="center" prop="naturalGas" />
      <!-- <el-table-column label="区域" align="center" prop="region" /> -->
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crm:supporting-usage:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:supporting-usage:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SupportingUsageForm ref="formRef" @success="getList" />
  <!-- 配套信息导入对话框 -->
  <SupportingUsageImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter3 } from '@/utils/formatTime'
import * as CustomerApi from '@/api/crm/customer'
import SupportingUsageImportForm from './SupportingUsageImportForm.vue'
import download from '@/utils/download'
import { SupportingUsageApi, SupportingUsageVO } from '@/api/crm/supportingusage'
import SupportingUsageForm from './SupportingUsageForm.vue'

/** CRM 企业配套用量信息 列表 */
defineOptions({ name: 'SupportingUsage' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SupportingUsageVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  customerId: undefined,
  fillingDate: [],
  pureWater: undefined,
  tapWater: undefined,
  totalWater: undefined,
  electricity: undefined,
  steam: undefined,
  naturalGas: undefined,
  region: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const customerList = ref<CustomerApi.CustomerVO[]>([]) // 客户列表
const { push } = useRouter()

/** 打开客户详情 */
const openCustomerDetail = (id: number) => {
  push({ name: 'CrmCustomerDetail', params: { id } })
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SupportingUsageApi.getSupportingUsagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SupportingUsageApi.deleteSupportingUsage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SupportingUsageApi.exportSupportingUsage(queryParams)
    download.excel(data, 'CRM 企业配套用量信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  customerList.value = await CustomerApi.getCustomerSimpleList()
})
</script>